package cn.com.dhcc.turbo.service.monitor.gather;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.handler.modulehandler.valuebean.BetweenValue;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.util.UUIDGenerator;
import cn.com.dhcc.turbo.entity.monitor.QuotaValueOnTime;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.service.exch.cmdata.SendCmDataService;
import cn.com.dhcc.turbo.service.exch.filedata.SendFileDataService;
import cn.com.dhcc.turbo.service.monitor.MonitorService;
import cn.com.dhcc.turbo.service.monitor.QUOTA;
import cn.com.dhcc.turbo.service.registry.NodeInfoService;
/**
 * 管理节点监控,节点间数据交换量
 * @日期：2014-10-10下午3:49:51
 * @作者：张洪涛
 * @版权所有：东华软件
 * @版本：1.0
 */
@Service
public class DataRealtimeGatherService {
	@SuppressWarnings("deprecation")
	Logger logger = Log.getExistLogger(Log.LOGTYPE.MONITOR);
	@Autowired
	NodeInfoService nodeInfoService;
	@Autowired
	private SendCmDataService sendCmDataService;
	@Autowired
    private SendFileDataService sendFileDataService;
    @Autowired
    private  MonitorService monitorService;
    /**
     * 数据发送
     */
    public void sendFlowHourForNode() {
		QUOTA quota = QUOTA.SEND_FLOW_HOUR_FOR_NODE;
		int num = 0;
		logger.info("开始收集指标[" + quota.getDesc() + "]...");
		try {
			
			NodeInfo localNode = nodeInfoService.getLocalNode();
			if(localNode==null){
				logger.error("收集指标[" + quota.getDesc() + "]失败:节点未初始化");
				return;
			}
			List<NodeInfo> nodeInfoList = nodeInfoService.getAllLocalExchNodeList(localNode.getId());
			Date now = new Date();
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(now);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
			calendar.set(Calendar.MILLISECOND, 0);
			calendar.add(Calendar.HOUR, -1);
			Date begintime = calendar.getTime();
			calendar.add(Calendar.HOUR, 1);
			Date endtime = calendar.getTime();
			
			for (NodeInfo nodeInfo : nodeInfoList) {
				if(localNode.getId().equals(nodeInfo.getId())){
					continue;
				}
				String nodecode = nodeInfo.getNodeCode();
				WhereCondition searchData = new WhereCondition();
				searchData
						.where()
						.eq("send_nodecode", localNode.getNodeCode())
						.and()
						.eq("recv_nodecode", nodecode)
						.and()
						.between("created",new BetweenValue(begintime, endtime));
				int num1 = sendCmDataService.count(searchData);
				int num2 = sendFileDataService.count(searchData);
				num = num1+num2;
				QuotaValueOnTime onTime = new QuotaValueOnTime();
				onTime.setQuotaCode(quota.getCode());
				onTime.setMtime(now);
				onTime.setId(UUIDGenerator.getUUID());
				onTime.setStatus(QuotaValueOnTime.QuotaHadlerStatus.WAIT_HANDLER.getCode());
				onTime.setOwnerId(localNode.getId()+"|"+nodeInfo.getId());
				onTime.setCurrentValue1("" + num);
				monitorService.insertQuotaOnTime(onTime);
			}
		} catch (Exception e) {
			logger.error("指标[" + quota.getDesc() + "]值收集失败", e);
		}
		logger.info("指标[" + quota.getDesc() + "]值收集结束，值为：" + num);
	}
}
